const db = require("../config/db");

/**
 * 栏目列表接口逻辑
 */
exports.columnSearch = (req, res) => {
  let sql =
    "select * from cms_column where del = 0 order by orderby ASC";
  db.query(sql, (err, result) => {
    if (err) {
      return res.send({ code: 1, message: err.message });
    }
    res.send({
      code: 0,
      data: {
        list: result,
      },
    });
  });
};

// 内容列表接口
exports.contentList = (req, res) => {
  let { Search, page, size, cms_columnId } = req.query;
  page = (page - 1) * size;
  let totalSql = "select count(*) as total from cms_content where del=0 ";
  let sql = "select * from cms_content where del = 0 ";
  if (Search == null && cms_columnId == null) {
    sql = sql + "order by orderby ASC limit ?, ?";
    argument = [Number(page), Number(size)];
    totalCenter = [];
  } else if (Search !== null && cms_columnId == null) {
    sql = sql + " and name = ? order by orderby ASC limit ?, ?";
    totalSql = totalSql + " and name = ?";
    argument = [Search, Number(page), Number(size)];
    totalCenter = [Search];
  } else if (Search == null && cms_columnId !== null) {
    sql = sql + " and cms_columnId = ? order by orderby ASC limit ?, ?";
    totalSql = totalSql + " and cms_columnId = ?";
    argument = [cms_columnId, Number(page), Number(size)];
    totalCenter = [cms_columnId];
  } else if (Search !== null && cms_columnId !== null) {
    sql =
      sql +
      " and name like  ? and cms_columnId = ? order by orderby ASC limit ?, ?";
    totalSql = totalSql + "and name like ? and cms_columnId = ?";
    argument = ["%" + Search + "%", cms_columnId, Number(page), Number(size)];
    totalCenter = ["%" + Search + "%", cms_columnId];
  }
  console.log(sql);
  
  db.query(sql, argument, (err, result) => {
    if (err) {
      return res.send({ code: 1, message: err.message });
    }
    db.query(totalSql, totalCenter, (err, resTotal) => {
      if (err) {
        return res.send({ code: 1, message: err.message });
      }
      res.send({
        code: 0,
        data: {
          list: result,
          totalList: resTotal,
        },
      });
    });
  });
};

/**
 * 内容单行列表接口逻辑
 */
exports.contentListOne = (req, res) => {
  let { id ,column_id} = req.query;
  let sql = "select * from cms_content where id = ? and cms_columnId = ?;";
  let goBackSql =
    "select * from cms_content where id < ? and cms_columnId = ? order by id desc limit 1;";
  let nextSql =
    "select * from cms_content where id > ? and cms_columnId = ? order by id asc limit 1;";
  let seeSql = "update cms_content set see = see + 1 where id = ?";
  db.query(sql, [id, column_id], (err, result) => {
    if (err) {
      return res.send({ code: 1, message: err.message });
    }
    db.query(goBackSql, [id, column_id], (err, goBack) => {
      if (err) {
        return res.send({ code: 1, message: err.message });
      }
      db.query(nextSql, [id, column_id], (err, next) => {
        if (err) {
          return res.send({ code: 1, message: err.message });
        }
        db.query(seeSql, id, (err, see) => {
          if (err) {
            return res.send({ code: 1, message: err.message });
          }
          res.send({
            code: 0,
            data: {
              list: result,
              goBack: goBack,
              next: next,
            },
          });
        });
      });
    });
  });
};
